package labox.innovation.loginserver.gameserverpackets;

/**
 * 客户游戏服务器发来的基础抽象包
 */
public abstract class ClientBasePacket {
	
	private byte[] _decrypt;

	private int _off;

	public ClientBasePacket(byte[] decrypt) {
		_decrypt = decrypt;
		_off = 1; //掠过最初的操作ID那个一个字节
	}

	public int readInt() {
		int result = _decrypt[_off++] & 0xff;
		result |= _decrypt[_off++] << 8 & 0xff00;
		result |= _decrypt[_off++] << 0x10 & 0xff0000;
		result |= _decrypt[_off++] << 0x18 & 0xff000000;
		return result;
	}

	public int readUnByte() {
		int result = _decrypt[_off++] & 0xff;
		return result;
	}

	public int readUnShort() {
		int result = _decrypt[_off++] & 0xff;
		result |= _decrypt[_off++] << 8 & 0xff00;
		return result;
	}

	public double readDouble() {
		long result = _decrypt[_off++] & 0xff;
		result |= _decrypt[_off++] << 8 & 0xff00;
		result |= _decrypt[_off++] << 0x10 & 0xff0000;
		result |= _decrypt[_off++] << 0x18 & 0xff000000;
		result |= _decrypt[_off++] << 0x20 & 0xff00000000l;
		result |= _decrypt[_off++] << 0x28 & 0xff0000000000l;
		result |= _decrypt[_off++] << 0x30 & 0xff000000000000l;
		result |= _decrypt[_off++] << 0x38 & 0xff00000000000000l;
		return Double.longBitsToDouble(result);
	}

	public String readString() {
		String result = null;
		try {
			result = new String(_decrypt, _off, _decrypt.length - _off, "UTF-16LE");
			result = result.substring(0, result.indexOf(0x00));
		} catch (Exception e) {
			e.printStackTrace();
		}
		_off += result.length() * 2 + 2;
		return result;
	}

	public final byte[] readBytes(int length) {
		byte[] result = new byte[length];
		for (int i = 0; i < length; i++) {
			result[i] = _decrypt[_off + i];
		}
		_off += length;
		return result;
	}
}
